package com.example.vueback.controller;

import com.example.vueback.config.AxiosResult;
import com.example.vueback.model.Empinfo;
import com.example.vueback.model.Userinfo;
import com.example.vueback.service.EmpService;
import com.example.vueback.service.UserService;
import com.example.vueback.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.coyote.ajp.AjpMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private EmpService empService;

    @RequestMapping("/sayHi")
    public String sayHi(String token) {
        if(StringUtils.hasLength(token)) {
            System.out.println(token);
        }
        return "hello!";
    }

    @RequestMapping("/login")
    public AxiosResult login(@RequestBody Userinfo userinfo) {
        //1.非空校验
        if(userinfo == null || !StringUtils.hasLength(userinfo.getUsername())
                || !StringUtils.hasLength(userinfo.getPassword())) {
            return AxiosResult.error("1-用户名或密码不能为空！");
        }
        //2.校验正确性
        Userinfo dbUserinfo = userService.getUserinfoByName(userinfo.getUsername());
        if(dbUserinfo == null || !dbUserinfo.getPassword().equals(userinfo.getPassword())) {
            return AxiosResult.error("2-用户名或密码错误");
        }
        //3.Token 存放在 redis 上
        Map<String, String> map = new HashMap<>();
        map.put(userinfo.getUsername(), userinfo.getUsername());
        String token = JwtUtils.createToken(map);
        redisTemplate.opsForValue().set(userinfo.getUsername(), token, 30, TimeUnit.MINUTES);
        //4.返回 Token
        return AxiosResult.success(token);
    }

    @RequestMapping("/add_or_update")
    public AxiosResult addOrupdate(@RequestBody Empinfo empinfo) {
        //1.非空校验
        if(empinfo == null || !StringUtils.hasLength(empinfo.getName()) ||
                empinfo.getAge() == null || empinfo.getSalary() == null) {
            return AxiosResult.error("3-参数不能为空!", -1);
        }
        //2.查看是否存在当前员工信息
        Empinfo dbEmpinfo = empService.getEmpinfoById(empinfo.getId());
        if(dbEmpinfo == null) {
            // 1) 不存在就，添加
            empService.addEmpinfo(empinfo);
        } else {
            // 1) 存在就修改
            empService.updateEmpinfo(empinfo);
        }
        return AxiosResult.success(1);
    }

    @RequestMapping("/list")
    public AxiosResult showEmpList() {
        List<Empinfo> empinfoAll = empService.getEmpinfoAll();
        return AxiosResult.success(empinfoAll);
    }

    @RequestMapping("/del")
    public AxiosResult delEmp(Integer id) {
        empService.delEmpinfoById(id);
        return AxiosResult.success(1);
    }

    @RequestMapping("/cur")
    public AxiosResult getCurUser(String username) {
        Userinfo userinfo = userService.getUserinfoByName(username);
        userinfo.setPassword("");
        return AxiosResult.success(userinfo);
    }

    @RequestMapping("/logout")
    public AxiosResult logout(String username) {
        //1.根据删除 redis 上的 token 信息
        Boolean isLogout = redisTemplate.delete(username);
        if(isLogout == null || !isLogout) {
            return AxiosResult.error("您的登录已过期~", -1);
        }
        log.info(username + " 用户退出登录!");
        return AxiosResult.success(1);
    }


}
